package com.inkyi.assistant.app.todolist;


import com.inkyi.assistant.app.todolist.event.TodoGroupDTO;
import com.inkyi.assistant.app.todolist.event.TodoRecordDTO;
import com.inkyi.assistant.app.todolist.left.GroupItem;
import com.inkyi.assistant.app.todolist.table.TodoItem;
import com.inkyi.assistant.core.todolist.entity.InkTodoGroup;
import com.inkyi.assistant.core.todolist.entity.InkTodoRecord;
import com.inkyi.assistant.core.todolist.service.InkTodoService;
import com.inkyi.assistant.core.utils.SpringUtils;
import com.inkyi.framework.common.lang.InkParams;
import org.springframework.data.domain.Page;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TodoServiceApi {

    public static final TodoGroupDTO DEFAULT_GROUP = new TodoGroupDTO(0L, "未分类", "default");

    private static InkTodoService inkTodoService;

    public static InkTodoService getInkTodoService() {
        if (inkTodoService == null) {
            inkTodoService = SpringUtils.getBean(InkTodoService.class);
        }
        return inkTodoService;
    }

    public static List<InkTodoGroup> findGroupList(InkParams params) {
        String title = params.get("title", String.class);
        Integer pageNum = params.getOrDefault("pageNum", 1, Integer.class);
        Integer pageSzie = params.getOrDefault("pageSzie", 100, Integer.class);
        Page<InkTodoGroup> page = getInkTodoService().groupList(title, pageNum, pageSzie);
        return page.toList();
    }

    public static List<InkTodoRecord> findRecordList(InkParams params) {
        Integer pageNum = params.getOrDefault("pageNum", 1, Integer.class);
        Integer pageSzie = params.getOrDefault("pageSzie", 100, Integer.class);
        Long groupId = params.get("groupId", Long.class);
        Map<String, Object> queryMap = new HashMap<>();
        if (TodoContext.GROUP_ID_JINTIAN.equals(groupId)) {
            queryMap.put("groupType", "jintian");
        } else if (TodoContext.GROUP_ID_ZHONGYAO.equals(groupId)) {
            queryMap.put("groupType", "zhongyao");
        } else if (TodoContext.GROUP_ID_YIWANCHENG.equals(groupId)) {
            queryMap.put("groupType", "yiwancheng");
        } else if (TodoContext.GROUP_ID_QUANBU.equals(groupId)) {
            queryMap.put("groupType", "quanbu");
        } else {
            queryMap.put("groupType", "group");
            queryMap.put("groupId", groupId);
        }
        Page<InkTodoRecord> page = getInkTodoService().recordList(queryMap, pageNum, pageSzie);
        return page.toList();
    }

    public static TodoGroupDTO convertDto(InkTodoGroup record) {
        TodoGroupDTO recordDTO = new TodoGroupDTO();
        recordDTO.setId(record.getId());
        recordDTO.setTitle(record.getTitle());
        recordDTO.setCode(record.getCode());
        recordDTO.setDescription(record.getDescription());
        recordDTO.setCreateTime(record.getCreateTime());
        recordDTO.setUpdateTime(record.getUpdateTime());
        recordDTO.setDescription(record.getDescription());
        recordDTO.setSort(record.getSort());
        return recordDTO;
    }

    public static TodoRecordDTO convertDto(InkTodoRecord record) {
        TodoRecordDTO recordDTO = new TodoRecordDTO();
        recordDTO.setId(record.getId());
        recordDTO.setTitle(record.getTitle());
        recordDTO.setDescription(record.getDescription());
        recordDTO.setCreateTime(record.getCreateTime());
        recordDTO.setUpdateTime(record.getUpdateTime());
        recordDTO.setGroupId(record.getGroupId());
        recordDTO.setParentId(record.getParentId());
        recordDTO.setSourceId(record.getSourceId());
        recordDTO.setContent(record.getContent());
        recordDTO.setPriority(record.getPriority());
        recordDTO.setSort(record.getSort());
        recordDTO.setTodoTime(record.getTodoTime());
        recordDTO.setReminderTime(record.getReminderTime());
        recordDTO.setDeadline(record.getDeadline());
        recordDTO.setRepeatRule(record.getRepeatRule());
        recordDTO.setFinish(record.getIsFinish() != null && record.getIsFinish());
        recordDTO.setDescription(record.getDescription());
        recordDTO.setTodoType(record.getTodoType());
        return recordDTO;
    }

    public static TodoRecordDTO findRecordOne(Long id) {
        InkTodoRecord record = getInkTodoService().recordOne(id);
        if (record == null) {
            return null;
        }
        return convertDto(record);
    }

    public static TodoGroupDTO findGroupOne(Long id) {
        InkTodoGroup record = getInkTodoService().groupOne(id);
        if (record == null) {
            return null;
        }
        return convertDto(record);
    }


    public static List<GroupItem> groupItemList(String title) {
        InkParams params = new InkParams();
        params.set("title", title);
        List<InkTodoGroup> groupList = findGroupList(params);
        List<GroupItem> groupItemList = new ArrayList<>();
        for (InkTodoGroup group : groupList) {
            GroupItem item = new GroupItem(group.getId(), group.getTitle());
            groupItemList.add(item);
        }
        return groupItemList;
    }

    public static List<TodoGroupDTO> groupDtoList() {
        InkParams params = new InkParams();
        List<InkTodoGroup> groupList = findGroupList(params);
        List<TodoGroupDTO> result = new ArrayList<>();
        result.add(DEFAULT_GROUP);
        for (InkTodoGroup group : groupList) {
            TodoGroupDTO item = new TodoGroupDTO();
            item.setId(group.getId());
            item.setTitle(group.getTitle());
            item.setCode(group.getCode());
            item.setCreateTime(group.getCreateTime());
            item.setUpdateTime(group.getUpdateTime());
            item.setDescription(group.getDescription());
            result.add(item);
        }
        return result;
    }

    public static List<TodoItem> todoItemList(Map<String, Object> params) {
        InkParams inkParams = new InkParams();
        params.forEach(inkParams::set);
        List<InkTodoRecord> recordList = findRecordList(inkParams);
        List<TodoItem> items = new ArrayList<>();
        for (InkTodoRecord itr : recordList) {
            boolean isFinish = itr.getIsFinish() != null && itr.getIsFinish();
            TodoItem item = new TodoItem(itr.getId(), isFinish, itr.getTitle());
            item.getParams().set("groupId", itr.getGroupId());
            items.add(item);
        }
        return items;
    }

    public static void groupSave(TodoGroupDTO item) {
        InkTodoGroup group = new InkTodoGroup();
        if (item.getId() == null) {
            group.setCreateTime(LocalDateTime.now());
        } else {
            group.setId(item.getId());
        }
        group.setTitle(item.getTitle());
        group.setUpdateTime(LocalDateTime.now());
        group.setCode(item.getCode());
        getInkTodoService().saveGroup(group);
        item.setId(group.getId());
        item.setCode(group.getCode());
    }

    public static void saveGroupByCode(TodoGroupDTO item) {
        InkTodoGroup group = new InkTodoGroup();
        group.setId(item.getId());
        group.setCode(item.getCode());
        group.setCreateTime(LocalDateTime.now());
        group.setUpdateTime(LocalDateTime.now());
        group.setTitle(item.getTitle());
        group.setSort(0);
        getInkTodoService().saveGroupByCode(group);
        item.setId(group.getId());
        item.setCode(group.getCode());
        item.setCreateTime(group.getCreateTime());
        item.setUpdateTime(group.getUpdateTime());
        item.setTitle(group.getTitle());
        item.setSort(group.getSort());
        item.setDescription(group.getDescription());
    }


    public static void groupDelete(Long id) {
        getInkTodoService().delGroup(id, false);
    }

    public static void recordSave(TodoRecordDTO item) {
        InkTodoRecord tr = new InkTodoRecord();
        if (item.getId() != null) {
            tr.setId(item.getId());
        }
        tr.setCreateTime(LocalDateTime.now());
        tr.setUpdateTime(LocalDateTime.now());
        tr.setParentId(item.getParentId());
        tr.setSourceId(item.getSourceId());
        tr.setTitle(item.getTitle());
        tr.setPriority(item.getPriority());
        tr.setSort(item.getSort());
        tr.setTodoTime(item.getTodoTime());
        tr.setGroupId(item.getGroupId());
        tr.setIsFinish(item.getFinish());
        tr.setTodoType(item.getTodoType());
        getInkTodoService().recordSave(tr);
        item.setId(tr.getId());
    }

    public static void recordDelete(Long id) {
        getInkTodoService().recordDel(id);
    }

    public static void attachmentList() {

    }

    public static void attachmentSave() {

    }

    public static void attachmentDelete() {

    }


    /**
     * 检查初始化数据
     *
     */
    public static void checkInitData() {
        saveGroupByCode(DEFAULT_GROUP);
    }
}
